﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using DevExpress.Utils;

public partial class UserControl_ControlReportPhanTuyen : System.Web.UI.UserControl
{
    ClassExecute.ClassExecute dt;
    string str = webapp4U.Configuration.CurrentConnectionString;
    string error = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            if (Session["db"] != null)
            {
                DataTable db = (DataTable)Session["db"];
                ASPxPivotGrid1.DataSource = db;
                ASPxPivotGrid1.DataBind();
            }
            if (Session["db1"] != null)
            {
                DataTable db1 = (DataTable)Session["db1"];
                ASPxPivotGrid2.DataSource = db1;
                ASPxPivotGrid2.DataBind();
            }
        }
        else
        {
            
            LoadFinalPivotResult();
        }
    }
    void ShowMessageBox(string text)
    {
        MessageBox.Text = text;
        MessageBox.ShowOnPageLoad = true;
    }
   
    void LoadFinalPivotResult()
    {
        dt = new ClassExecute.ClassExecute();
        try
        {
           

            DataTable db = dt.Get_Data_DataTable(str, ref error, "[K_getTDVSetRouteReport]", CommandType.StoredProcedure,
               new SqlParameter("@Username", Session["user"].ToString()));

            if (db.Rows.Count > 0)
            {

                grvTDV.DataSource = db;

            }
            else
            {

                grvTDV.DataSource = null;
                ShowMessageBox("Không có Trình dược viên nào được phân tuyến");
            }
            grvTDV.DataBind();
        }
        catch (Exception ex)
        {
            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        }
    }
    protected void imgBtnAdd_Click(object sender, ImageClickEventArgs e)
    {
        LoadFinalPivotResult();
        TabContainer1.ActiveTabIndex = 0;
    }
    
    [STAThread]
    void ReportCust(DataTable db)
    {
        if (db.Rows.Count > 0)
        {

            TabContainer1.ActiveTabIndex = 1;
            ASPxPivotGrid1.DataSource = db;
            Session["db"] = db;
        }
        else
        {
            if (Session["db"] != null)
            {
                Session["db"] = null;
            }
            ASPxPivotGrid1.DataSource = null;
            ShowMessageBox("Không có dữ liệu");
        }
        ASPxPivotGrid1.DataBind();
    }
    [STAThread]
    void ReporSKU(DataTable db)
    {
        if (db.Rows.Count > 0)
        {

            ASPxPivotGrid2.DataSource = db;
            Session["db1"] = db;
        }
        else
        {
            if (Session["db1"] != null)
            {
                Session["db1"] = null;
            }

            ASPxPivotGrid2.DataSource = null;
           
        }
        ASPxPivotGrid2.DataBind();
    }
    protected void grvTDV_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        dt = new ClassExecute.ClassExecute();
        try
        {
            if (e.CommandName == "chon")
            {

                GridViewRow grv = ((Control)e.CommandSource).NamingContainer as GridViewRow;
                Label lblMaSo = (Label)grv.FindControl("lblsalesp_key");
                Label lblHoTen = (Label)grv.FindControl("lblsalesp_name");

                DataTable db = dt.Get_Data_DataTable(str, ref error, "k_getReportRouteSetting", CommandType.StoredProcedure,
                    new SqlParameter("@salesp_key", lblMaSo.Text));

                DataTable db1 = dt.Get_Data_DataTable(str, ref error, "k_getReportSKUSetting", CommandType.StoredProcedure,
                    new SqlParameter("@salesp_key", lblMaSo.Text));


                lblTDV1.Text = lblMaSo.Text;
                lblHotenTDV1.Text = lblHoTen.Text;

                lblTDV.Text = lblMaSo.Text;
                lblHotenTDV.Text = lblHoTen.Text;

                for (int i = 0; i < ASPxPivotGrid1.Fields.Count; i++)
                {
                    ASPxPivotGrid1.Fields[i].FilterValues.Clear();
                }
                for (int i = 0; i < ASPxPivotGrid2.Fields.Count; i++)
                {
                    ASPxPivotGrid2.Fields[i].FilterValues.Clear();
                }

                ReportCust(db);
                ReporSKU(db1);
            }
        }
        catch (Exception ex)
        {

            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        }
    }
}